package TestDemo;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;


@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class TestCases extends InitAndEnd{
    /**
     * 登录
     */
    @Order(1)
     @ParameterizedTest
    @CsvSource(value = "zhangsan,234567")//多参数
    void Login(String username,String password) throws InterruptedException {
        sleep(1);
        webDriver.get("http://101.42.226.71:8080/blog_login.html");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#userName")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

        //校验登录
        Set<String> handles= webDriver.getWindowHandles();
        String target="";
        for(String handle:handles){
            target=handle;
        }
        webDriver.switchTo().window(target);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String actual_username=webDriver.findElement(By.cssSelector("h3")).getText();
        Assertions.assertEquals(username,actual_username);
    }

    /**
     * 测试获取博客列表
     */
    @Order(2)
    @Test
    void BolgList(){
        webDriver.get("http://101.42.226.71:8080/blog_list.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        int row= webDriver.findElements(By.cssSelector(".title")).size();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertNotEquals(0,row);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String title= webDriver.getTitle();//获取加载栏标题
        Assertions.assertEquals("博客列表页",title);
    }

    /**
     * 测试点击查看博客详情是否为空
     */
    @Order(3)
    @Test
    void BolgDetail(){
        //打开博客列表页
        webDriver.get("http://101.42.226.71:8080/blog_list.html");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //点击查看全文
        webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
        //查看博客详情页是否为空
        String detail="";
        detail=webDriver.findElement(By.cssSelector("#content > p")).getText();
        Assertions.assertNotEquals("",detail);
    }

    /**
     * 写博客
     */
    @Order(4)
    @Test

    void EditBlog() throws InterruptedException {
       //定位写博客按钮并点击
       webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
       webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
       //定位搜索框并输入“自动化测试”
        (  (JavascriptExecutor)webDriver).executeScript("document.querySelector(\"#title\").value=\"自动化测试2\"");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //定位并点击发布博客
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //校验首页第一篇是否是新发布的博客
        String first_blog_title=webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(12) > div.title")).getText();
        String cur_url=webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        System.out.println(cur_url);
        Assertions.assertEquals("自动化测试2",first_blog_title);
    }

    /**
     * 删除博客
     */
    @Order(5)
    @Test
    void DeleteBlog() throws InterruptedException {
        //打开博客页
        webDriver.get("http://101.42.226.71:8080/blog_list.html");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //找到查看全文，点击
        webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(11) > a")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String cur_url2= webDriver.getCurrentUrl();
        //找到删除按钮，点击
        webDriver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")).click();
        //验证删除后页面跳转与博客时间
        webDriver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        String blog_create_time=webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(11) > div.date")).getText();

        if(blog_create_time.contains(" 19:58:08")){
            System.out.println("测试不通过");

        }else{
            System.out.println("测试通过");

        }
    }

    /**
     * 退出博客
     */
    @Order(6)
    @Test
    void Logout(){

        //找到注销按钮点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //验证是否退出到登录页面
       String login_text = webDriver.findElement(By.cssSelector("body > div.container-login > div > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
       if(login_text.equals("登陆")){
           System.out.println("测试通过");
       }else{
           System.out.println(login_text);
           System.out.println("测试不通过");
       }

    }
}
